package com.ufs.cheftalk.activity.qb

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Typeface
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import android.text.TextUtils
import android.util.TypedValue
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.annotation.DimenRes
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.constraintlayout.widget.ConstraintSet
import androidx.core.content.ContextCompat
import androidx.databinding.BindingAdapter
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ItemDecoration
import com.jbt.mds.obdsdk.R
import com.jbt.mds.obdsdk.extension.BindingFunction



@BindingAdapter(
    value = ["horWeight","weight"],
    requireAll = false
)
fun aaa(tv:View,view: ConstraintLayout,weight:Float){
    val constraintSet = ConstraintSet()
    constraintSet.clone(view.context, R.layout.three_text_info_layout)
    constraintSet.setHorizontalWeight(tv.id,weight)
    view.setConstraintSet(constraintSet)
}


//圆角 drawable 边框 角度 颜色
@BindingAdapter(
    value = ["color", "corner_radius", "stroke_width", "stroke_color"],
    requireAll = false
)
fun strokeGradientBackground(
    view: View,
    color: Int,
    cornerRadius: Int = 0,
    strokeWidth: Int,
    strokeColor: Int
) {
    val gradientDrawable = GradientDrawable()
    gradientDrawable.shape = GradientDrawable.RECTANGLE
    try {
        val colorRes = ContextCompat.getColor(view.context, strokeColor)
        gradientDrawable.setStroke(strokeWidth, colorRes)
    } catch (e: Exception) {
        gradientDrawable.setStroke(strokeWidth, strokeColor)
    }
    gradientDrawable.cornerRadius = cornerRadius.toFloat()
    gradientDrawable.setColor(ContextCompat.getColor(view.context, color))
    view.background = gradientDrawable
}










@BindingAdapter(value = ["layoutManager"])
fun setLayoutManager(
    recyclerView: RecyclerView,
    function: BindingFunction<RecyclerView, RecyclerView.LayoutManager>?
) {
    recyclerView.layoutManager = function?.apply(recyclerView)
}




@BindingAdapter(
    value = ["textStyle"]
)
fun textViewTextStyle(textView: TextView, textStyle: Int) {
    textView.setTypeface(null,textStyle)
}

@BindingAdapter(
    value = ["textSizeDimen"]
)
fun textViewTextSizeDimen(textView: TextView, textSize: Int) {
    textView.setTextSize(
        TypedValue.COMPLEX_UNIT_PT,
        textView.context.getPtDimen(textSize).toFloat()
    )
}

@BindingAdapter("textSizeSp")
fun bindTextSize(textView: TextView, size: Int) {
    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, size.toFloat())
}

fun Context.getPtDimen(@DimenRes res: Int): Int = resources.getDimension(res).toInt()



@BindingAdapter(
    value = ["bindSrcCompat"],
    requireAll = false
)
fun bind(imageView: ImageView, drawRes: Int) {
    if (drawRes != 0) {
        imageView.setImageResource(drawRes)
    }
}



/**
 * 字体是否变粗
 */
@BindingAdapter("isBold")
fun setBold(view: TextView, isBold: Boolean) {
    if (isBold) {
        view.setTypeface(null, Typeface.BOLD)
    } else {
        view.setTypeface(null, Typeface.NORMAL)
    }
}

@BindingAdapter("itemDecoration")
fun setItemDecoration(
    view: RecyclerView, itemDecoration: ItemDecoration?,
) {

    itemDecoration?.let {
       val itemDecorationCount = view.itemDecorationCount
        if(itemDecorationCount>0) {
            for (i in 0 until itemDecorationCount) {
                view.removeItemDecorationAt(i)
            }
        }
        view.addItemDecoration(itemDecoration)
    }

}

